Thursday, September 09, 2010

Exhaustive, Average PnL % [EXAPLP]

Descripton
Summary:
This optimization script runs the portfolio with all possible argument combinations and calculates the Average PnL % at the end of each run.
The script guarantees that the optimal set of parameter arguments will be found.


Variables
TypeIdentifierDescription
Integer_maxVectorsUse for the maximum number of optimization vectors.
Integer_vectorLengthGet the number of items in the optimization vector.
Boolean_findMaximumUse for indicating whether to find the maximum or minimum score.

OnInitialize
Function Parameters
TypeIdentifierDescription
BooleanfindMaximumUse for indicating whether to find the maximum or minimum score. [Default True]
BooleanuseTradesUse for indicating whether to use trades or position statistics. [Default True]
Implementation
	 ' Assign the script parameter to a script variable.
	_findMaximum = findMaximum
	 ' Get the number of items in the optimization vector.
	_vectorLength = OptimizationVectorItemCount()
	 ' Count the maximum number of possible optimization vectors.
	_maxVectors = 1
	For i As Integer = 0 To _vectorLength - 1
		_maxVectors *= OptimizationVectorItemOptionCount(i)
	Next
	 ' Define the statistics request that is used to calculate the optimization goal.
	StatsRequest(-1,DateTimeStart(),DateTimeEnd(),True,True,True,True,useTrades)

OnMaxVectors
Function Parameters
TypeIdentifierDescription
Implementation
	 ' Return the maximum number of optimization vectors.
	Return _maxVectors

OnScoreVector
Function Parameters
TypeIdentifierDescription
Implementation
	 ' Calculate the optimization goal.
	If (_findMaximum) Then
		Return StatsAverageProfitLossPercent()
	Else 
		Return -1 * StatsAverageProfitLossPercent()
	End If

OnSelectVector
Function Parameters
TypeIdentifierDescription
Implementation
	 ' Use for the result optimization vector.
	Define result(_vectorLength - 1) As Number
	 ' If this isn 't the first run then read the last optimization vector.
	If (OptimizationVectorCount() > 0) Then
		result = OptimizationVector(OptimizationVectorCount() - 1)
	Else
	 ' If this is the first run then initialize and return the first vector with the lowest values.
		For itemIndex As Integer = 0 To _vectorLength - 1
			result(itemIndex) = OptimizationVectorItemLow(itemIndex)
		Next
		Return result
	End If

	 ' Iterate over all of the vector items while increasing the vector by one step.
	For itemIndex As Integer = 0 To _vectorLength - 1
		 ' Use for the highest value of the current vector item.
		Define itemHigh As Number = OptimizationVectorItemHigh(itemIndex)
		 ' Use for the lowest value of the current vector item.
		Define itemLow As Number = OptimizationVectorItemLow(itemIndex)
		 ' Use for the step value value of the current vector item.
		Define itemStep As Number = OptimizationVectorItemStep(itemIndex)

		 ' If the current vector item can be increased by one step then do so.
		If (result(itemIndex) + itemStep <= itemHigh) Then
			result(itemIndex) = result(itemIndex) + itemStep
			Exit For
		Else
		 ' otherwise reset the current vector item to its lower value and try to increase the next item.
			result(itemIndex) = itemLow
		End If
	Next
	Return result

Copyright © 2010 IQBroker, LLC. All rights reserved.